#!/bin/sh
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


# OPTIONS:
#   -f: start in foreground
#   -e: start with elasticsearch enabled
#   -p <filename>: log the pid to a file (useful to kill it later)
#   -v: print version string and exit

# CONTROLLING STARTUP:
# 
# This script relies on few environment variables to determine startup
# behavior, those variables are:
#
#   CLASSPATH -- A Java classpath containing everything necessary to run.
#   JVM_OPTS -- Additional arguments to the JVM for heap size, etc
#   JVM_ON_OUT_OF_MEMORY_ERROR_OPT -- The OnOutOfMemoryError JVM option if specified
#   CASSANDRA_CONF -- Directory containing Cassandra configuration files.
#   CASSANDRA_LOGDIR -- Directory containing cassandra logs, override default location
#   CASSANDRA_DAEMON -- the cassandra entry point class:
#        "org.apache.cassandra.service.ElassandraDaemon" to enable elasticsearch
#
# As a convenience, a fragment of shell is sourced in order to set one or
# more of these variables. This so-called `include' can be placed in a 
# number of locations and will be searched for in order. The highest 
# priority search path is the same directory as the startup script, and
# since this is the location of the sample in the project tree, it should
# almost work Out Of The Box.
#
# Any serious use-case though will likely require customization of the
# include. For production installations, it is recommended that you copy
# the sample to one of /usr/share/cassandra/cassandra.in.sh,
# /usr/local/share/cassandra/cassandra.in.sh, or 
# /opt/cassandra/cassandra.in.sh and make your modifications there.
#
# Another option is to specify the full path to the include file in the
# environment. For example:
#
#   $ CASSANDRA_INCLUDE=/path/to/in.sh cassandra -p /var/run/cass.pid
#
# Note: This is particularly handy for running multiple instances on a 
# single installation, or for quick tests.
#
# Finally, developers and enthusiasts who frequently run from an SVN 
# checkout, and do not want to locally modify bin/cassandra.in.sh, can put
# a customized include file at ~/.cassandra.in.sh.
#
# If you would rather configure startup entirely from the environment, you
# can disable the include by exporting an empty CASSANDRA_INCLUDE, or by 
# ensuring that no include files exist in the aforementioned search list.
# Be aware that you will be entirely responsible for populating the needed
# environment variables.

# NB: Developers should be aware that this script should remain compatible with
# POSIX sh and Solaris sh. This means, in particular, no $(( )) and no $( ).

# If an include wasn't specified in the environment, then search for one...
if [ "x$CASSANDRA_INCLUDE" = "x" ]; then
    # Locations (in order) to use when searching for an include file.
    for include in "`dirname "$0"`/cassandra.in.sh" \
                   "$HOME/.cassandra.in.sh" \
                   /usr/share/cassandra/cassandra.in.sh \
                   /usr/local/share/cassandra/cassandra.in.sh \
                   /opt/cassandra/cassandra.in.sh; do
        if [ -r "$include" ]; then
            . "$include"
            break
        fi
    done
# ...otherwise, source the specified include.
elif [ -r "$CASSANDRA_INCLUDE" ]; then
    . "$CASSANDRA_INCLUDE"
fi

# Use JAVA_HOME if set, otherwise look for java in PATH
if [ -n "$JAVA_HOME" ]; then
    # Why we can't have nice things: Solaris combines x86 and x86_64
    # installations in the same tree, using an unconventional path for the
    # 64bit JVM.  Since we prefer 64bit, search the alternate path first,
    # (see https://issues.apache.org/jira/browse/CASSANDRA-4638).
    for java in "$JAVA_HOME"/bin/amd64/java "$JAVA_HOME"/bin/java; do
        if [ -x "$java" ]; then
            JAVA="$java"
            break
        fi
    done
else
    JAVA=java
fi

if [ -z $JAVA ] ; then
    echo Unable to find java executable. Check JAVA_HOME and PATH environment variables. >&2
    exit 1;
fi

# If numactl is available, use it. For Cassandra, the priority is to
# avoid disk I/O. Even for the purpose of CPU efficiency, we don't
# really have CPU<->data affinity anyway. Also, empirically test that numactl
# works before trying to use it (CASSANDRA-3245).
NUMACTL_ARGS=${NUMACTL_ARGS:-"--interleave=all"}
if which numactl >/dev/null 2>/dev/null && numactl $NUMACTL_ARGS ls / >/dev/null 2>/dev/null
then
    NUMACTL="numactl $NUMACTL_ARGS"
else
    NUMACTL=""
fi

if [ -z "$CASSANDRA_CONF" -o -z "$CLASSPATH" ]; then
    echo "You must set the CASSANDRA_CONF and CLASSPATH vars" >&2
    exit 1
fi

if [ -f "$CASSANDRA_CONF/cassandra-env.sh" ]; then
    . "$CASSANDRA_CONF/cassandra-env.sh"
fi

# Special-case path variables.
case "`uname`" in
    CYGWIN*|MINGW*) 
        CLASSPATH=`cygpath -p -w "$CLASSPATH"`
        CASSANDRA_CONF=`cygpath -p -w "$CASSANDRA_CONF"`
    ;;
esac

# Set default log dir if not set
if [ -z "$CASSANDRA_LOGDIR" ]; then
  for logdir in "/var/log/cassandra" "$CASSANDRA_HOME/logs"; do
    if [ -d "$logdir" ]; then
      CASSANDRA_LOGDIR="$logdir"
      break
    fi
  done
fi

# Cassandra uses an installed jemalloc via LD_PRELOAD / DYLD_INSERT_LIBRARIES by default to improve off-heap
# memory allocation performance. The following code searches for an installed libjemalloc.dylib/.so/.1.so using
# Linux and OS-X specific approaches.
# To specify your own libjemalloc in a different path, configure the fully qualified path in CASSANDRA_LIBJEMALLOC.
# To disable jemalloc preload at all, set CASSANDRA_LIBJEMALLOC=-
#
#CASSANDRA_LIBJEMALLOC=
#
find_library()
{
    pattern=$1
    path=$(echo ${2} | tr ":" " ")

    find $path -regex "$pattern" -print 2>/dev/null | head -n 1
}
case "`uname -s`" in
    Linux)
        if [ -z $CASSANDRA_LIBJEMALLOC ] ; then
            which ldconfig > /dev/null 2>&1
            if [ $? = 0 ] ; then
                # e.g. for CentOS
                dirs="/lib64 /lib /usr/lib64 /usr/lib `ldconfig -v 2>/dev/null | grep -v '^\s' | sed 's/^\([^:]*\):.*$/\1/'`"
            else
                # e.g. for Debian, OpenSUSE
                dirs="/lib64 /lib /usr/lib64 /usr/lib `cat /etc/ld.so.conf /etc/ld.so.conf.d/*.conf | grep '^/'`"
            fi
            dirs=`echo $dirs | tr " " ":"`
            CASSANDRA_LIBJEMALLOC=$(find_library '.*/libjemalloc\.so\(\.1\)*' $dirs)
        fi
        if [ ! -z $CASSANDRA_LIBJEMALLOC ] ; then
            export JVM_OPTS="$JVM_OPTS -Dcassandra.libjemalloc=$CASSANDRA_LIBJEMALLOC"
            if [ "-" != "$CASSANDRA_LIBJEMALLOC" ] ; then
                export LD_PRELOAD=$CASSANDRA_LIBJEMALLOC
            fi
        fi
    ;;
    Darwin)
        if [ -z $CASSANDRA_LIBJEMALLOC ] ; then
            CASSANDRA_LIBJEMALLOC=$(find_library '.*/libjemalloc\.dylib' $DYLD_LIBRARY_PATH:${DYLD_FALLBACK_LIBRARY_PATH-$HOME/lib:/usr/local/lib:/lib:/usr/lib})
        fi
        if [ ! -z $CASSANDRA_LIBJEMALLOC ] ; then
            export JVM_OPTS="$JVM_OPTS -Dcassandra.libjemalloc=$CASSANDRA_LIBJEMALLOC"
            if [ "-" != "$CASSANDRA_LIBJEMALLOC" ] ; then
                export DYLD_INSERT_LIBRARIES=$CASSANDRA_LIBJEMALLOC
            fi
        fi
    ;;
esac

launch_service()
{
    pidpath="$1"
    foreground="$2"
    props="$3"
    class="$4"
    cassandra_parms="-Dlogback.configurationFile=$CASSANDRA_CONF/logback.xml"
    cassandra_parms="$cassandra_parms -Dcassandra.logdir=$CASSANDRA_LOGDIR"
    cassandra_parms="$cassandra_parms -Dcassandra.storagedir=$cassandra_storagedir"

    # needed by elaticsearch for graphic computations
    JVM_OPTS="$JVM_OPTS -Djava.awt.headless=true"

    if [ "x$pidpath" != "x" ]; then
        cassandra_parms="$cassandra_parms -Dcassandra-pidfile=$pidpath"
    fi

    echo "Starting cassandra with $class"

    # Problem with ElassandraDaemon if CASSANDRA_HOME and CASSANDRA_CONF are not in environment
    # We have to export explicitly CASSANDRA_HOME and CASSANDRA_CONF
    [ -n "$CASSANDRA_HOME" ] && export CASSANDRA_HOME
    [ -n "$CASSANDRA_CONF" ] && export CASSANDRA_CONF


    # The cassandra-foreground option will tell CassandraDaemon not
    # to close stdout/stderr, but it's up to us not to background.
    if [ "x$foreground" != "x" ]; then
        cassandra_parms="$cassandra_parms -Dcassandra-foreground=yes"
        if [ "x$JVM_ON_OUT_OF_MEMORY_ERROR_OPT" != "x" ]; then
            exec $NUMACTL "$JAVA" $JVM_OPTS "$JVM_ON_OUT_OF_MEMORY_ERROR_OPT" $cassandra_parms -cp "$CLASSPATH" $props "$class"
        else
            exec $NUMACTL "$JAVA" $JVM_OPTS $cassandra_parms -cp "$CLASSPATH" $props "$class"
        fi
    # Startup CassandraDaemon, background it, and write the pid.
    else
        if [ "x$JVM_ON_OUT_OF_MEMORY_ERROR_OPT" != "x" ]; then
            exec $NUMACTL "$JAVA" $JVM_OPTS "$JVM_ON_OUT_OF_MEMORY_ERROR_OPT" $cassandra_parms -cp "$CLASSPATH" $props "$class" <&- &
            [ ! -z "$pidpath" ] && printf "%d" $! > "$pidpath"
            true
        else
            exec $NUMACTL "$JAVA" $JVM_OPTS $cassandra_parms -cp "$CLASSPATH" $props "$class" <&- &
            [ ! -z "$pidpath" ] && printf "%d" $! > "$pidpath"
            true
        fi
    fi

    return $?
}

# Parse any command line options.
args=`getopt vRefdhp:bD:H:E: "$@"`
eval set -- "$args"

while true; do
    case "$1" in
        -p)
            pidfile="$2"
            shift 2
        ;;
        -f)
            foreground="yes"
            shift
        ;;
        -h)
            echo "Usage: $0 [-f] [-h][-e] [-p pidfile] [-H dumpfile] [-E errorfile]"
            exit 0
        ;;
        -v)
            "$JAVA" -cp "$CLASSPATH" "-Dlogback.configurationFile=logback-tools.xml" org.apache.cassandra.tools.GetVersion
            exit 0
        ;;
        -R)
            allow_root="yes"
            shift
        ;;
        -D)
            properties="$properties -D$2"
            shift 2
        ;;
        -e)
            elastic="yes"
            shift 1
        ;;
        -d) # start in debug mode
            echo "Starting with JVM debug address=${JVM_DEBUG_PORT:-4242} suspend=${JVM_DEBUG_WAIT:-n}"
            JVM_OPTS="$JVM_OPTS -ea -Xdebug -Xnoagent -Xrunjdwp:transport=dt_socket,server=y,suspend=${JVM_DEBUG_WAIT:-n},address=${JVM_DEBUG_PORT:-4242}"
            shift 1
        ;;
        -H)
            properties="$properties -XX:HeapDumpPath=$2"
            shift 2
        ;;
        -E)
            properties="$properties -XX:ErrorFile=$2"
            shift 2
        ;;
        --)
            shift
            if [ "x$*" != "x" ] ; then
                echo "Error parsing arguments! Unknown argument \"$*\"" >&2
                exit 1
            fi
            break
        ;;
        *)
            echo "Error parsing arguments! Unknown argument \"$1\"" >&2
            exit 1
        ;;
    esac
done


if  [ "$elastic" = "yes" ]; then
  classname="org.apache.cassandra.service.ElassandraDaemon"
elif [ -n "$CASSANDRA_DAEMON" ]; then
  classname="$CASSANDRA_DAEMON"
else
  classname="org.apache.cassandra.service.CassandraDaemon"
fi

if [ "x$allow_root" != "xyes" ] ; then
    if [ "`id -u`" = "0" ] || [ "`id -g`" = "0" ] ; then
        echo "Running Cassandra as root user or group is not recommended - please start Cassandra using a different system user."
        echo "If you really want to force running Cassandra as root, use -R command line option."
        exit 1
    fi
fi

# Start up the service
launch_service "$pidfile" "$foreground" "$properties" "$classname"

exit $?

# vi:ai sw=4 ts=4 tw=0 et
